package com.keshe.order.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keshe.common.utils.PageUtils;
import com.keshe.common.utils.Query;
import org.apache.commons.lang.StringUtils;
import com.keshe.order.dao.OrderDao;
import com.keshe.order.entity.OrderEntity;
import com.keshe.order.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

  /*  @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }*/
    @Autowired
    OrderDao orderDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        wrapper
            .eq("is_deleted", 0)
            .like(StringUtils.isNotBlank(key), "order_sn", key);

        IPage<OrderEntity> page = this.page(new Query<OrderEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    public OrderEntity getOrderDatails(Integer orderId) {
        QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.order_id", orderId);
        return orderDao.getOrderDatails(wrapper);
    }

    @Override
    public IPage<OrderEntity> getOrderList(Map<String, Object> params) {
        long current = Long.valueOf((String) params.get("page")).longValue();
        long size = Long.valueOf((String) params.get("limit")).longValue();
        Page<OrderEntity> page = new Page<>(current, size);

        String orderSn = (String) params.get("orderSn");
        String account = (String) params.get("account");
        String createTime = (String) params.get("createTime");
        String orderStatus = (String) params.get("orderStatus");
        QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotBlank(orderSn), "order_sn", orderSn)
                .eq(StringUtils.isNotBlank(account), "t2.account", account)
                .likeRight(StringUtils.isNotBlank(createTime), "t1.create_time", createTime)
                .eq(StringUtils.isNotBlank(orderStatus), "order_status", orderStatus);

        return orderDao.getOrderList(page, wrapper);
    }

    public PageUtils SearchQueryPage(Map<String, Object> params) {
        String orderSn = (String)params.get("orderSn");
        String userid = (String)params.get("userName");
        String createTime = (String)params.get("createTime");
        String orderStatus = (String)params.get("orderStatus");
        System.out.println(orderSn + "  " + userid + "  " + createTime + "  " + orderStatus);
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        wrapper
                .eq("is_deleted", 0)
                .eq(StringUtils.isNotBlank(orderSn), "order_sn", orderSn)
                .eq(StringUtils.isNotBlank(userid), "user_id", userid)
                .likeLeft(StringUtils.isNotBlank(createTime), "create_time", createTime)

                .eq(StringUtils.isNotBlank(orderStatus), "order_status", orderStatus);


        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}
